package com.akk.module.message;

import com.akk.exception.RestBusinessException;
import com.akk.utils.Constants;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @company www.lovekaka.com
 * @auther hainlc
 * @create 2017/05/24 17:27
 */
@Component
public class NormalMessageService extends MessageService {

    private static NormalMessageService normalMessageService;
    public NormalMessageService(){

    }

    public static NormalMessageService getInstance() {
        if(normalMessageService == null)
            normalMessageService = new NormalMessageService();
        return normalMessageService;
    }

    private String getPwd() {
        try {
            return getMD5(Constants.MSG_SERIAL_NUMBER+ Constants.MSG_PASS);
        } catch (UnsupportedEncodingException e) {
            throw new RestBusinessException("短信发送异常");
        }
    }


    @Override
    public String sendSpecialMessage(String mobile, String content, String ext, String stime, String rrid, String msgfmt) throws UnsupportedEncodingException {
        try {
            return sendNormalMessage(mobile,  content,  ext,  stime,
                    rrid, msgfmt);
        } catch (IOException e) {
            throw new RestBusinessException("短信发送异常");
        }
    }

    /*
         * 方法名称：mdsmssend
         * 功    能：发送短信
         * 参    数：mobile,content,ext,stime,rrid,msgfmt(手机号，内容，扩展码，定时时间，唯一标识，内容编码)
         * 返 回 值：唯一标识，如果不填写rrid将返回系统生成的
         */
    public String sendNormalMessage(String mobile, String content, String ext, String stime,
                                   String rrid,String msgfmt) throws IOException {
        String result = "";
        String soapAction = "http://entinfo.cn/mdsmssend";
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
        xml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
        xml += "<soap:Body>";
        xml += "<mdsmssend  xmlns=\"http://entinfo.cn/\">";
        xml += "<sn>" + Constants.MSG_SERIAL_NUMBER+ "</sn>";
        xml += "<pwd>" +getPwd()+ "</pwd>";
        xml += "<mobile>" + mobile + "</mobile>";
        xml += "<content>" + content + "</content>";
        xml += "<ext>" + ext + "</ext>";
        xml += "<stime>" + stime + "</stime>";
        xml += "<rrid>" + rrid + "</rrid>";
        xml += "<msgfmt>" + msgfmt + "</msgfmt>";
        xml += "</mdsmssend>";
        xml += "</soap:Body>";
        xml += "</soap:Envelope>";

        ByteArrayOutputStream bout = null;
        OutputStream out = null;
        InputStreamReader isr = null;
        BufferedReader in = null;
        URL url;
        try {
            url = new URL(Constants.MSG_URL);

            URLConnection connection = url.openConnection();
            HttpURLConnection httpconn = (HttpURLConnection) connection;
            bout = new ByteArrayOutputStream();
            bout.write(xml.getBytes());
            byte[] b = bout.toByteArray();
            httpconn.setRequestProperty("Content-Length", String
                    .valueOf(b.length));
            httpconn.setRequestProperty("Content-Type",
                    "text/xml; charset=gb2312");
            httpconn.setRequestProperty("SOAPAction", soapAction);
            httpconn.setRequestMethod("POST");
            httpconn.setDoInput(true);
            httpconn.setDoOutput(true);
            out = httpconn.getOutputStream();
            out.write(b);
            out.close();
            isr = new InputStreamReader(httpconn
                    .getInputStream());
            in = new BufferedReader(isr);
            String inputLine;
            while (null != (inputLine = in.readLine())) {
                Pattern pattern = Pattern.compile("<mdsmssendResult>(.*)</mdsmssendResult>");
                Matcher matcher = pattern.matcher(inputLine);
                while (matcher.find()) {
                    result = matcher.group(1);
                }
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }finally {
            if (bout!=null) {
                bout.close();
            }
            if(out!=null){
                out.close();
            }
            if(isr!=null){
                isr.close();
            }
            if(in!=null){
                in.close();
            }
        }
    }


}
